package com.gid.rcflightlogger;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableRow;
import android.widget.TextView;


public class BatteryBrowseActivity extends InventoryBrowseBaseActivity {

	private class BatteryData
	{
		public Battery mBattery;
		private int mCyclesCount;
		private int mRunTime; //seconds
		
		BatteryData(Battery b)
		{
			mBattery = b;
			mCyclesCount = 0;
			mRunTime = 0;
			
			//load cycle data from DB
			List<Cycle> cycles = mBattery.getCycles(getApplicationContext()).orderBy("mDate").toList();
			for (Cycle c : cycles)
			{
				mCyclesCount += c.getCount();
				mRunTime += c.getDuration();
			}
		}
		
		public int getCyclesCount()
		{
			return mCyclesCount;
		}
		
		public int getRunTime()
		{
			return mRunTime;
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		PREF_PREFIX = "BatteryBrowse";
        super.onCreate(savedInstanceState);
        mCurrentMenuID = R.id.menu_main_inventory_batteries;
        LOG_TAG = "BatteryBrowse";
        mAdd.setText("Add Battery");
	}
	
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
	
	// ====================================================
	// Override methods from base inventory browse class
	// ====================================================
	
	@Override
	protected TableRow renderHeaders()
	{
		TableRow header = (TableRow) getLayoutInflater().inflate(R.layout.layout_batterybrowse_header, null);
		return header;
	}

	@Override
	protected void spawnItemEditor(int request_code, int id)
	{
		Intent i = new Intent(this, BatteryEditActivity.class);
		
		if (request_code == REQUEST_CODE_EDIT && id != 0)
    	{
			i.putExtra(Battery.ID_LABEL, id);
			//myUtils.makeToast(getApplicationContext(), "id = " + Integer.toString(id));
    	}
		startActivityForResult(i, request_code);
	}

	@Override
	protected void loadData()
	{
		//reload data from database
		List<Battery> allbatteries = Battery.objects(getApplicationContext()).all().orderBy("-mStatus").toList();
		mData.clear();
		
		for (Battery b : allbatteries)
		{
			BatteryData bd = new BatteryData(b);
			mData.add(bd);
		}
	}
	
	@Override
	protected int doCompare(Object lhs, Object rhs)
	{
		int res = 0;
		assert(lhs instanceof BatteryData);
		assert(rhs instanceof BatteryData);
		
		BatteryData lb =  (BatteryData) lhs;
		BatteryData rb =  (BatteryData) rhs;
		
		if (sortColumn >= 0 && sortColumn < COLUMN_COUNT) {
			if (sortColumn == 0) // tag
				res = lb.mBattery.getTag().compareTo(rb.mBattery.getTag());
			else if (sortColumn == 1) // age
				res = (-1)*(lb.mBattery.getPurchaseDate().compareTo(rb.mBattery.getPurchaseDate()));
			else if (sortColumn == 2) // cost
				res = (int) (lb.mBattery.getCost() - rb.mBattery.getCost());
			else if (sortColumn == 3) // cycles
				res = lb.getCyclesCount() - rb.getCyclesCount();
			else if (sortColumn == 4) // run time
				res = lb.getRunTime() - rb.getRunTime();

			if (sortUp)
				res *= -1;
		}
		else //no sort, use db order
		{
			res = lb.mBattery.getId() - rb.mBattery.getId();
		}

		return res;
	}

	@Override
	protected TableRow renderRow(Object od)
	{
		TableRow tr;
		TextView tv1, tv2, tv3, tv4, tv5;
		BatteryData bd = (BatteryData) od;

		tr = (TableRow) getLayoutInflater().inflate(R.layout.layout_batterybrowse_row, null);
		tr.setId(bd.mBattery.getId());
		
		// Fill out cells
		tv1 = (TextView) tr.findViewById(R.id.batterybrowse_col_name);
		tv1.setGravity(Gravity.LEFT);
		tv1.setText(bd.mBattery.getTag());
		
		tv2 = (TextView) tr.findViewById(R.id.batterybrowse_col_age);
		tv2.setGravity(Gravity.RIGHT);
		tv2.setText(myUtils.makeAgeStr(bd.mBattery.getPurchaseDate()));
		
		tv3 = (TextView) tr.findViewById(R.id.batterybrowse_col_cost);
		tv3.setGravity(Gravity.RIGHT);
		tv3.setText(Double.toString(bd.mBattery.getCost()));
	    
		tv4 = (TextView) tr.findViewById(R.id.batterybrowse_col_cycles);
		tv4.setGravity(Gravity.RIGHT);
		tv4.setText(Integer.toString(bd.getCyclesCount())); 
		
		tv5 = (TextView) tr.findViewById(R.id.batterybrowse_col_runtime);
		tv5.setGravity(Gravity.RIGHT);
		tv5.setText(myUtils.Duration2Str(bd.getRunTime(), true)); 
		
		//fade text if the item is deactivated
		if (!bd.mBattery.getStatus())
		{
			tv1.setTextColor(Colors.TEXT_INACTIVE_ITEM);
			tv2.setTextColor(Colors.TEXT_INACTIVE_ITEM);
			tv3.setTextColor(Colors.TEXT_INACTIVE_ITEM);
			tv4.setTextColor(Colors.TEXT_INACTIVE_ITEM);
			tv5.setTextColor(Colors.TEXT_INACTIVE_ITEM);
		}
		
		return tr;
	}
}
